﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;


namespace WebSite.Ajax
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string username = Request.QueryString["username"] + "";
            string password = Request.QueryString["password"] + "";
            string expire = Request.QueryString["expire"] + "";

            UsersHelper uhelper = new UsersHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UserName", username);
            c.Add(CriteriaType.Equals, "Password", Encrypt.Encode(password));
            c.Add(CriteriaType.Equals, "RoleType", 1);
            List<Users> userlist = uhelper.QueryUsersBySearch(c, null);

            if (userlist != null && userlist.Count > 0)
            {
                Users user = userlist[0];

                if (user.Flag == 0)
                {
                    //更新此用户的最后登录日期
                    user.LastAccessDate = System.DateTime.Now;
                    uhelper.EditUsers(user);

                    
                    Session["CurrentUser"] = user;
                    //写入cookie
                    //if (expire != "0" || expire != "")
                    //{
                    //    HttpCookie cookie = new HttpCookie("USER_COOKIE");
                    //    cookie.Values.Add("username", user.UserName);
                    //    cookie.Values.Add("password", user.Password);
                    //    cookie.Expires = System.DateTime.Now.AddDays(30.0);
                    //    HttpContext.Current.Response.Cookies.Add(cookie);
                    //}

                    HttpCookie cookie = new HttpCookie("USER_COOKIE");
                    cookie.Values.Add("username", user.UserName);
                    cookie.Values.Add("password", user.Password);
                    cookie.Expires = System.DateTime.Now.AddDays(30.0);
                    HttpContext.Current.Response.Cookies.Add(cookie);

                    Response.Write("succ");
                    return;
                }
                else//账号被禁用
                {
                    Response.Write("jinyong");
                    return;
                }
            }
            else
            {
                Response.Write("fail");
                return;
            }
        }
    }
}